package projeto1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author Nicholas
 */

/*Classe LeitorHtml, 
 * Objetos dessa classe contem metodos para ler um arquivo html
*/
public class LeitorHtml {
    
        public void lerHtml(String catalogo,String curso){
		if(Integer.parseInt(catalogo)>=2012){
			CatalogoTipo1("temp.html", curso);
		}
		else{
			CatalogoTipo0("temp.html", curso);
		}
	}

	public void CatalogoTipo1(String CatalogoFile,String curso){
		BufferedReader br = null;
		BufferedWriter writer = null;
		File file = new File ("temp.txt");
		
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			br = new BufferedReader(new InputStreamReader(new FileInputStream(CatalogoFile),"ISO-8859-1"));
			writer =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	    try {
	        String line="",linebefore="";
	        int noline = 0;
	        int linhascomtexto=0;
	        boolean lendodisciplinas=false;
	        String lendodisciplinastipo = "";
	        String puretext;
	        char GrupoDeEletiva = 'A';

	        while ((line = br.readLine()) != null) {
	        	//if(LinhaComTexto(line))
	        	//	System.out.println(line);
	        	if(RemoveTags(line).toLowerCase().contains(curso) && RemoveTags(line).toLowerCase().contains("Currículo Pleno".toLowerCase())  && LinhaComTexto(line)){
	        		line=RemoveTags(line);
	        		line=line.replace(curso+" - ","");	
	        		line=line.replace(" - Currículo Pleno","");
	        		//System.out.println(line);
	        		writer.write(line+"\n");
	        	}
	        	if(line.toLowerCase().contains("Núcleo Comum ao Curso".toLowerCase())){
	        		lendodisciplinas=true;
        			lendodisciplinastipo="PLE";
	        	}
        		else if(line.toLowerCase().contains("Além do núcleo comum".toLowerCase())){
        			//System.out.println();
        			//System.out.println(RemoveTags(linebefore));
        			writer.write("\n");
        			writer.write(RemoveTags(linebefore)+"\n");
        			lendodisciplinas=true;
        			lendodisciplinastipo="MOD";
        		}
        		else if(line.toLowerCase().contains("créditos dentre".toLowerCase())){
        			lendodisciplinas=true;
        			lendodisciplinastipo=new StringBuilder().append(GrupoDeEletiva).append(line.charAt(0)).append(line.charAt(1)).toString();
        			GrupoDeEletiva++;
        		}
	        	if(lendodisciplinas){
		            line = br.readLine();
		            while(LinhaComTexto(line=br.readLine()) && (ContemDisciplina(puretext = RemoveTags(line)) || puretext.contains("Qualquer disciplina"))){
		            	if(ContemDisciplina(puretext)){
		            		//System.out.println(lendodisciplinastipo+" "+puretext);
		            		writer.write(lendodisciplinastipo+" "+puretext+"\n");
		            		noline++;
		            	}
		            	else if(puretext.contains("Qualquer disciplina")){
		            		writer.write(lendodisciplinastipo+" XX000 Qualquer Disciplina"+"\n");
		            		//System.out.println(lendodisciplinastipo+" XX000 Qualquer Disciplina");
		            		noline++;
		            	}
		            }
		            lendodisciplinas=false;
	        	}
	        	linebefore=line;
	        }
	        //System.out.println();
	        //System.out.println("Número de disciplinas: "+noline);
	        br.close();
	        writer.close();
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void CatalogoTipo0(String CatalogoFile, String curso){
		BufferedReader br = null;
		BufferedWriter writer = null;
		File file = new File ("temp.txt");
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(CatalogoFile),"ISO-8859-1"));
			writer =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	    try {
	        String line="",linebefore="";
	        int noline = 0;
	        int linhascomtexto=0;
	        boolean lendodisciplinas=false;
	        String lendodisciplinastipo = "";
	        String puretext;
	        char GrupoDeEletiva = 'A';

	        while ((line = br.readLine()) != null) {
	        	//if(LinhaComTexto(line))
	        	//		System.out.println(line);
	        	if(RemoveTags(line).toLowerCase().contains(curso) && RemoveTags(line).toLowerCase().contains("Currículo".toLowerCase()) && LinhaComTexto(line) && !(line.contains("<title>"))){
	        		line=RemoveTags(line);
	        		if(curso.length()==1)
	        			line=line.replace("curso 0"+curso+" - ","");
	        		else{
	        			line=line.replace("curso "+curso+" - ","");	
	        		}
	        		line=line.replace(" - Currículo","");
	        		//System.out.println(line);
	        		writer.write(line+"\n");
	        	}
	        	if(line.toLowerCase().contains("Núcleo Comum ao Curso".toLowerCase())){
	        		lendodisciplinas=true;
        			lendodisciplinastipo="PLE";
	        	}
        		else if(line.toLowerCase().contains("Além do núcleo comum".toLowerCase()) || line.toLowerCase().contains("Além do nucleo comum".toLowerCase()) ){
        			//System.out.println();
        			//System.out.println(RemoveTags(linebefore));
        			writer.write("\n");
        			writer.write(RemoveTags(linebefore)+"\n");
        			lendodisciplinas=true;
        			lendodisciplinastipo="MOD";
        		}
        		else if(line.toLowerCase().contains("créditos dentre".toLowerCase())){
        			lendodisciplinas=true;
        			lendodisciplinastipo=new StringBuilder().append(GrupoDeEletiva).append(RemoveTags(line).charAt(0)).append(RemoveTags(line).charAt(1)).toString();
        			GrupoDeEletiva++;
        		}
	        	if(lendodisciplinas){
		            while(lendodisciplinas){ 
		            	if(LinhaComTexto(line=line=br.readLine())){
			            	puretext = RemoveTags(line);
			            	if(ContemDisciplina(RemoveSpecialChars(puretext))){
				            	//System.out.println(lendodisciplinastipo+" "+RemoveSpecialChars(puretext));
				            	writer.write(lendodisciplinastipo+" "+RemoveSpecialChars(puretext)+"\n");
				            	noline++;
				            }
				            else if(puretext.contains("Qualquer disciplina oferecida")){
				            	//System.out.println(lendodisciplinastipo+" XX000 Qualquer Disciplina");
				            	writer.write(lendodisciplinastipo+" XX000 Qualquer Disciplina"+"\n");
				            	noline++;
				            }
				            else if(puretext.contains("Qualquer disciplina com")){
				            	//System.out.println(lendodisciplinastipo+" "+RemoveSpecialChars(puretext));
				            	writer.write(lendodisciplinastipo+" "+RemoveSpecialChars(puretext)+"\n");
				            	noline++;
				            }
				            else{
				            	lendodisciplinas = false;
				            }
		            	}
		            }
		            
	        	}
	        	linebefore=line;
	        }
	        //System.out.println();
	        //System.out.println("Número de disciplinas: "+noline);
	        br.close();
	        writer.close();
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean LinhaComTexto(String line){
		int i;
		boolean insidetag=false;
		
		for(i=0;i<line.length();i++){
			if(insidetag==false && line.charAt(i)!='<' && line.charAt(i)!=' '){
				return true;
			}
			if(line.charAt(i)=='<')
				insidetag=true;
			if(line.charAt(i)=='>')
				insidetag=false;
		}
		return false;
	}
	public String RemoveTags(String line){
		int i;
		boolean insidetag=false;
		String newline="";
		
		for(i=0;i<line.length();i++){
			if(insidetag==false && line.charAt(i)!='<'){
				newline=newline+line.charAt(i);
			}
			if(line.charAt(i)=='<')
				insidetag=true;
			if(line.charAt(i)=='>')
				insidetag=false;
		}
		return newline;
	}
	
	public boolean ContemDisciplina(String line){
		if(line.length()>2){
			if(Character.isLetter(line.charAt(0)) && (Character.isLetter(line.charAt(1)) || line.charAt(1) ==' ') 
					&& Character.isDigit(line.charAt(2)) && Character.isDigit(line.charAt(3)) && Character.isDigit(line.charAt(4))){
				return true;
			}
		}
		return false;
	}
	
	public String RemoveSpecialChars(String line){
		String newline;
		
		
		newline=line.replaceAll("&nbsp;"," ");
		
		return newline;
	}
}